package com.house.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.house.dto.RoomDTO;
import com.house.entity.Room;
import com.house.reportisy.RoomRepository;
import com.house.service.RoomService;
import com.house.vo.RoomVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;
import java.util.Optional;

/**
 * @author: GGP
 * @description: 房间service实现类
 * @time: 2023-04-15 16:39
 **/
@Service
public class RoomServiceImpl implements RoomService {

	@Resource
	private RoomRepository roomRepository;

	@Override
	public Page<Room> findAll(RoomDTO roomDTO) {
		Pageable pageable = PageRequest.of(roomDTO.getPage() - 1, roomDTO.getSize());
		return roomRepository.findAll(new Specification<Room>() {
			@Override
			public Predicate toPredicate(Root<Room> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// 动态 SQL表达式
				Predicate predicate = criteriaBuilder.conjunction();
				// 动态 SQL表达式集合
				List<Expression<Boolean>> expressions = predicate.getExpressions();
					expressions.add(criteriaBuilder.equal(root.get("houseId"), roomDTO.getHouseId()));
				if (roomDTO.getRoomStatus()!=null){
					expressions.add(criteriaBuilder.equal(root.get("roomStatus"),0));
				}
				expressions.add(criteriaBuilder.equal(root.get("delFlag"), 0));
				query.orderBy(criteriaBuilder.desc(root.get("createDate")));
				return predicate;
			}
		}, pageable);
	}

	@Override
	public RoomVO findRoomByNum(RoomDTO roomDTO) {
		Optional<Room> one = roomRepository.findOne(new Specification<Room>() {
			@Override
			public Predicate toPredicate(Root<Room> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// 动态 SQL表达式
				Predicate predicate = criteriaBuilder.conjunction();
				// 动态 SQL表达式集合
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				expressions.add(criteriaBuilder.equal(root.get("houseId"), roomDTO.getHouseId()));
				expressions.add(criteriaBuilder.equal(root.get("number"), roomDTO.getNumber()));
				expressions.add(criteriaBuilder.equal(root.get("delFlag"), 0));
				return predicate;
			}
		});
		Room room = one.orElse(null);
		if (room == null) {
			return null;
		}
		RoomVO roomVO = new RoomVO();
		BeanUtil.copyProperties(room, roomVO);
		return roomVO;
	}

	@Override
	public Room saveRoom(RoomDTO roomDTO) {
		Room room = new Room();
		BeanUtil.copyProperties(roomDTO, room, "id");
		room.setRoomStatus(0);
		return roomRepository.save(room);
	}

	@Override
	public Room update(Integer id,Integer roomStatus) {
		Optional<Room> byId = roomRepository.findById(id);
		Room room = byId.orElse(null);
		Room updateRoom = new Room();
		BeanUtil.copyProperties(room,updateRoom);
		if (roomStatus==null){
			updateRoom.setDelFlag(1);
		}
		updateRoom.setRoomStatus(roomStatus);
		return roomRepository.save(updateRoom);
	}

	@Override
	public Room findRoomById(Integer id) {
		Optional<Room> byId = roomRepository.findById(id);
		return byId.orElse(null);
	}
}
